﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BoilerplateWPF.Helpers
{
    /// <summary>
    /// 配置文件 帮助类
    /// </summary>
    internal class ConfigHelper
    {
        /// <summary>
        /// 获取设置好的参数
        /// </summary>
        /// <param name="key">string类型，传入配置文件设置好的键</param>
        /// <returns></returns>
        public static string GetValue(string key)
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            if (config.AppSettings.Settings[key] == null)
                return "";
            else
                return config.AppSettings.Settings[key].Value;
        }

        /// <summary>
        /// 设置配置文件参数
        /// </summary>
        /// <param name="key">string类型，设置相应键值对的键</param>
        /// <param name="value">string类型，设置相应键值对的值</param>
        public static void SetValue(string key, string value)
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            if (config.AppSettings.Settings[key] == null)
            {
                config.AppSettings.Settings.Add(key, value);
            }
            else
            {
                config.AppSettings.Settings[key].Value = value;
            }
            config.Save(System.Configuration.ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");//重新加载新的配置文件 
        }
    }
}
